package edu.cmu.cs.cs214.hw9.client.gui;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;

import javax.swing.*;

import edu.cmu.cs.cs214.hw9.client.controllers.Event;
import edu.cmu.cs.cs214.hw9.client.controllers.FacelookEvent;
import edu.cmu.cs.cs214.hw9.datatypes.User;

/**
 * Creates the default registration screen. 
 */
public class RegistrationScreen extends FacelookScreen {
	private static final long serialVersionUID = 8790658635916627074L;

	/**
	 * Actionlistener to register a user into the database, based on info read from the form.
	 */
	private class RegisterUser implements ActionListener {
		private JTextField emailField;
		private JTextField nameField;
		private JPasswordField passwordField;
		private JPasswordField confirmPassField;
		
		public RegisterUser(JTextField emailField, JTextField nameField,
							JPasswordField passwordField, JPasswordField confirmPassField) {
			this.emailField = emailField;
			this.nameField = nameField;
			this.passwordField = passwordField;
			this.confirmPassField = confirmPassField;
		}
		
		public void actionPerformed(ActionEvent e) {
			if (Arrays.equals(passwordField.getPassword(), confirmPassField.getPassword())) {
				User newUser = new User(emailField.getText(), nameField.getText(), String.valueOf(passwordField.getPassword()));
				fireEventOccured(new FacelookEvent(Event.REGISTER_NEW_USER, newUser));
			} else {
				JOptionPane.showMessageDialog(RegistrationScreen.this, "Passwords do not match!", "Registration Error", JOptionPane.ERROR_MESSAGE);
			}
		}
	}
	
	/**
	 * Constructs a new facelook registration screen using forms.
	 */
	public RegistrationScreen() {
		// Layout init.
		GridBagConstraints c = new GridBagConstraints();
		
		// Labels to label form fields.
		JLabel registerLabel = new JLabel("Register for Facelook!");
		JLabel nameLabel = new JLabel("Full Name: ");
		JLabel emailLabel = new JLabel("Email Address: ");
		JLabel passwordLabel = new JLabel("Password: ");
		JLabel confirmPassLabel = new JLabel("Confirm Password: ");
		
		// Make labels have white font.
		registerLabel.setForeground(Color.white);
		nameLabel.setForeground(Color.white);
		emailLabel.setForeground(Color.white);
		passwordLabel.setForeground(Color.white);
		confirmPassLabel.setForeground(Color.white);
		
		// Form fields.
		JTextField nameField = new JTextField();
		JTextField emailField = new JTextField();
		JPasswordField passwordField = new JPasswordField();
		JPasswordField confirmPassField = new JPasswordField();
		
		// Init form field size to something reasonable., like ~20 columns.
		nameField.setColumns(30);
		emailField.setColumns(30);
		passwordField.setColumns(20);
		confirmPassField.setColumns(20);
		
		// Buttons!
		JButton registerButton = new JButton("Register");
		JButton loginButton = new JButton("Back to Login");
		
		// Make the register button add a new user to the database. 
		registerButton.addActionListener(new RegisterUser(emailField, nameField, passwordField, confirmPassField));
		
		// Make the "back to login" button go back to the login screen.
		loginButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				fireEventOccured(new FacelookEvent(Event.GO_TO_LOGIN, "Login"));
			}
		});
		
		// Add the labels and fields and buttons to the layout. 
		// 1st Row
		c.gridx = 0;
		c.gridwidth = 2;
		add(registerLabel, c);
		
		// 2nd Row
		c.anchor = GridBagConstraints.WEST;
		c.gridwidth = 1;
		add(nameLabel, c);
		
		c.gridx = 1;
		add(nameField, c);

		// 3rd Row
		c.gridx = 0;
		add(emailLabel, c);
		
		c.gridx = 1;
		add(emailField, c);

		// 4th Row
		c.gridx = 0;
		add(passwordLabel, c);
		
		c.gridx = 1;
		add(passwordField, c);
		
		// 5th Row
		c.gridx = 0;
		add(confirmPassLabel, c);
		
		c.gridx = 1;
		add(confirmPassField, c);
		
		// 6th Row
		c.anchor = GridBagConstraints.CENTER;
		c.gridx = 0;
		add(registerButton, c);
		
		c.gridx = 1;
		add(loginButton, c);
	}
}
